package com.kly.cashmall.module.products.presenter;


import com.kly.cashmall.bean.ProductSubmitFilterBean;
import com.kly.cashmall.bean.ProductCategoryBean;
import com.kly.cashmall.bean.UserProfileListBean;
import com.kly.cashmall.framework.mvp.BaseViewPresenter;
import com.kly.cashmall.services.Network;
import com.kly.cashmall.services.Params;
import com.kly.cashmall.widget.Loading.LoadingDialog;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;


/**
 * @author
 */
public class ProductDetailPresenter extends BaseViewPresenter<ProductDetailViewer> {

    public ProductDetailPresenter(ProductDetailViewer viewer) {
        super(viewer);
    }

    public void getProductDetail(String categoryCode, String merchantCode) {
        LoadingDialog.showLoading(getActivity());
        Params params = new Params();
        params.put("categoryCode", categoryCode);
        params.put("merchantCode", merchantCode);
        rxManager.add(Network.getApi().getProductDetail(params.buildPostBody()).subscribeOn(Schedulers.io()).observeOn
                (AndroidSchedulers.mainThread()).subscribe(new Consumer<ProductCategoryBean>() {
            @Override
            public void accept(ProductCategoryBean bean) throws Exception {
                LoadingDialog.dismissLoading();
                if (getViewer() != null) {
                    getViewer().setProductDetail(bean);
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                LoadingDialog.dismissLoading();
            }
        }));

        getUserProfileList(categoryCode, merchantCode);
    }

    public void getUserProfileList(String productCategory, String merchantCode) {
        Params params = new Params();
        params.put("productCategory", productCategory);
        params.put("merchantCode", merchantCode);
        rxManager.add(Network.getApi().getUserProfileActionList(params.buildPostBody()).subscribeOn(Schedulers.io()).observeOn
                (AndroidSchedulers.mainThread()).subscribe(new Consumer<UserProfileListBean>() {
            @Override
            public void accept(UserProfileListBean bean) throws Exception {
                if (getViewer() != null) {
                    getViewer().setUserProfileList(bean);
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {

            }
        }));
    }

    public void getOrderSubmitFilter(String productCode, String merchantCode) {
        LoadingDialog.showLoading(getActivity());
        Params params = new Params();
        params.put("productCode", productCode);
        params.put("merchantCode", merchantCode);
        rxManager.add(Network.getApi().getOrderSubmitFilter(params.buildPostBody()).subscribeOn(Schedulers.io()).observeOn
                (AndroidSchedulers.mainThread()).subscribe(new Consumer<ProductSubmitFilterBean>() {
            @Override
            public void accept(ProductSubmitFilterBean bean) throws Exception {
                LoadingDialog.dismissLoading();
                if (getViewer() != null) {
                    getViewer().getOrderSubmitPostSuccess(bean);
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                LoadingDialog.dismissLoading();
                if (getViewer()!= null){
                    getViewer().getOrderSubmitPostFail(throwable.getMessage());
                }
            }
        }));

    }


    @Override
    public void willDestroy() {

    }
}